<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="golang,">










<meta name="description" content="interface 接口12345678910111213141516171819202122232425262728293031323334353637//https://learnku.com/articles/25488type Pet interface &amp;#123;    SetName(name string)    Name() string    Category() string">
<meta name="keywords" content="golang">
<meta property="og:type" content="article">
<meta property="og:title" content="golang 学习笔记">
<meta property="og:url" content="http://yoursite.com/2019/03/18/golang-学习笔记/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="interface 接口12345678910111213141516171819202122232425262728293031323334353637//https://learnku.com/articles/25488type Pet interface &amp;#123;    SetName(name string)    Name() string    Category() string">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-11T09:26:57.251Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="golang 学习笔记">
<meta name="twitter:description" content="interface 接口12345678910111213141516171819202122232425262728293031323334353637//https://learnku.com/articles/25488type Pet interface &amp;#123;    SetName(name string)    Name() string    Category() string">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/03/18/golang-学习笔记/">



<meta name="referrer" content="never"> ​​​​


  <title>golang 学习笔记 | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/03/18/golang-学习笔记/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">golang 学习笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-03-18T20:39:59+08:00">
                2019-03-18
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  7.9k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  38 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="interface-接口"><a href="#interface-接口" class="headerlink" title="interface 接口"></a>interface 接口</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//https://learnku.com/articles/25488</span></span><br><span class="line">type Pet interface &#123;</span><br><span class="line">    SetName(name string)</span><br><span class="line">    Name() string</span><br><span class="line">    Category() string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Dog struct &#123;</span><br><span class="line">    name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog *Dog) SetName(name string) &#123;</span><br><span class="line">    dog.name = name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog Dog) Name() string &#123;</span><br><span class="line">    <span class="keyword">return</span> dog.name</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (dog Dog) Category() string &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"dog"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func TestDog(t *testing.T) &#123;</span><br><span class="line">    dog := Dog&#123;<span class="string">"little pig"</span>&#125;</span><br><span class="line">    _, <span class="attr">ok</span> := interface&#123;&#125;(dog).(Pet)</span><br><span class="line">    fmt.Printf(<span class="string">"Dog implements interface Pet: %v\n"</span>, ok) <span class="comment">// Dog implements interface Pet: false</span></span><br><span class="line">    _, ok = interface&#123;&#125;(&amp;dog).(Pet)</span><br><span class="line">    fmt.Printf(<span class="string">"*Dog implements interface Pet: %v\n"</span>, ok) <span class="comment">// *Dog implements interface Pet: true</span></span><br><span class="line">    <span class="keyword">var</span> pet Pet = &amp;dog</span><br><span class="line">    fmt.Printf(<span class="string">"This pet is a %s, the name is %q.\n"</span>,</span><br><span class="line">        pet.Category(), pet.Name()) <span class="comment">//  This pet is a dog, the name is "little pig".</span></span><br><span class="line"></span><br><span class="line">    dog.SetName(<span class="string">"monster"</span>)</span><br><span class="line">    fmt.Printf(<span class="string">"This pet is a %s, the name is %q.\n"</span>,</span><br><span class="line">        pet.Category(), pet.Name()) <span class="comment">// This pet is a dog, the name is "monster".</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Go-陷阱之-for-循环迭代变量"><a href="#Go-陷阱之-for-循环迭代变量" class="headerlink" title="Go 陷阱之 for 循环迭代变量"></a>Go 陷阱之 for 循环迭代变量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26861</span></span><br><span class="line">func main() &#123;</span><br><span class="line">    sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">        fmt.Println(&amp;v)</span><br><span class="line">        slice = append(slice, func()&#123;</span><br><span class="line">            fmt.Println(v * v) <span class="comment">// 直接打印结果</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">        val()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 25 25 25 25 25</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">        temp := v <span class="comment">// 其实很简单 引入一个临时局部变量就可以了，这样就可以将每次的值存储到该变量地址上</span></span><br><span class="line">        fmt.Println(&amp;temp) <span class="comment">// 这里内存地址是不同的</span></span><br><span class="line">        slice = append(slice, func()&#123;</span><br><span class="line">            fmt.Println(temp *  temp) <span class="comment">// 直接打印结果</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">        val()</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 1, 4, 9, 16, 25 预期结果</span></span><br><span class="line"><span class="keyword">for</span> i, <span class="attr">v</span> := range</span><br><span class="line">i, v 都是只创建一次，然后循环中赋值。</span><br><span class="line">另外，循环的数组或 <span class="built_in">Map</span>，是在开始前的镜像，循环中添加或移除元素不改变其循环次数。</span><br><span class="line">关于循环还可以引申的是 <span class="built_in">Map</span> 时是无序的</span><br></pre></td></tr></table></figure>
<h3 id="Struct-与-面向对象"><a href="#Struct-与-面向对象" class="headerlink" title="Struct 与 面向对象"></a>Struct 与 面向对象</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">type Human struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">var</span> tom Human</span><br><span class="line"><span class="comment">// 通过赋值初始化 https://learnku.com/articles/25094</span></span><br><span class="line">tom.name, tom.age = <span class="string">"Tom"</span>, <span class="number">18</span></span><br><span class="line"><span class="comment">// 详细初始化</span></span><br><span class="line">jerry := Human&#123;<span class="attr">age</span>:<span class="number">25</span>, <span class="attr">name</span>:<span class="string">"Jerry"</span>&#125;</span><br><span class="line"><span class="comment">// 按照结构体声明顺序</span></span><br><span class="line">peter := Human&#123;<span class="string">"Peter"</span>, <span class="number">34</span>&#125;</span><br><span class="line"><span class="comment">// 通过 . 访问</span></span><br><span class="line">fmt.Printf(<span class="string">"%s is %d old\n"</span>, tom.name, tom.age)</span><br><span class="line"></span><br><span class="line">type Staff struct &#123;</span><br><span class="line">    Human <span class="comment">// 隐式的引入 Human 的字段</span></span><br><span class="line">    wage int</span><br><span class="line">    age float32 <span class="comment">// 覆盖 Human 的 age</span></span><br><span class="line">&#125;</span><br><span class="line">tom := Staff&#123;Human&#123;<span class="string">"Tom"</span>, <span class="number">18</span>&#125;, <span class="number">1000</span>, <span class="number">18.5</span>&#125;</span><br><span class="line">jerr := Staff&#123;<span class="attr">Human</span>:Human&#123;<span class="attr">age</span>:<span class="number">32</span>, <span class="attr">name</span>:<span class="string">"Jerr"</span>&#125;, <span class="attr">wage</span>: <span class="number">2000</span>, <span class="attr">age</span>: <span class="number">32.5</span>&#125;</span><br><span class="line">fmt.Printf(<span class="string">"%s is %f old，%d years, wage is %d"</span>, tom.name, tom.Human.age, tom.age, tom.wage)</span><br><span class="line">fmt.Printf(<span class="string">"%s is %f old，%d years, wage is %d"</span>, jerr.name, jerr.Human.age, jerr.age, jerr.wage)</span><br><span class="line">type Human struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Employee struct &#123;</span><br><span class="line">    Human</span><br><span class="line">    wage int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (h *Human) Say() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Hi, I am %s, My age is %d"</span>, h.name, h.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// Employee 重写 Say</span></span><br><span class="line">func (e *Employee) Say() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Hi, I am %s, %d old year, wage is %d per mothn"</span>, e.name, e.age, e.wage)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">func TestPanic(t *testing.T) &#123;</span><br><span class="line">    defer func() &#123;</span><br><span class="line">        fmt.Println(<span class="string">"最后结果依旧执行!"</span>) <span class="comment">// 这一部分的代码依旧执行</span></span><br><span class="line">    &#125;()</span><br><span class="line">    fmt.Println(<span class="string">"执行开始"</span>)</span><br><span class="line">    panic(errors.New(<span class="string">"错误信息!"</span>))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出大致结果如下</span></span><br><span class="line">开始</span><br><span class="line">最后结果依旧执行!</span><br><span class="line">--- FAIL: TestPanic (<span class="number">0.00</span>s)</span><br><span class="line">panic: 错误信息! [recovered]</span><br><span class="line">panic: 错误信息!</span><br></pre></td></tr></table></figure>
<h3 id="rand-Intn-生成的是伪随机数"><a href="#rand-Intn-生成的是伪随机数" class="headerlink" title="rand.Intn () 生成的是伪随机数"></a>rand.Intn () 生成的是伪随机数</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">rand.Intn() 函数是个伪随机函数, 不管运行多少次都只会返回同样的随机数, 因为它默认的资源就是单一值, 所以必须调用 rand.Seed(), 并且传入一个变化的值作为参数, 如 time.Now().UnixNano() , 就是可以生成时刻变化的值.</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (<span class="string">"fmt"</span></span><br><span class="line">        <span class="string">"math/rand"</span></span><br><span class="line">        <span class="string">"time"</span>)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="comment">// 初始化随机数的资源库, 如果不执行这行, 不管运行多少次都返回同样的值https://learnku.com/articles/26011</span></span><br><span class="line">    rand.Seed(time.Now().UnixNano())</span><br><span class="line">    fmt.Println(<span class="string">"A number from 1-100"</span>, rand.Intn(<span class="number">81</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="beego-框架-model-curd"><a href="#beego-框架-model-curd" class="headerlink" title="beego 框架 model curd"></a>beego 框架 model curd</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br></pre></td><td class="code"><pre><span class="line">定义要连接的数据库 main.go https:<span class="comment">//learnku.com/articles/26029#topnav</span></span><br><span class="line"></span><br><span class="line">package  main</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  _  <span class="string">"newgo/routers"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">  _  <span class="string">"github.com/go-sql-driver/mysql"</span></span><br><span class="line">)</span><br><span class="line">func  init()  &#123;</span><br><span class="line">  orm.RegisterDriver(<span class="string">"mysql"</span>,  orm.DRMySQL)</span><br><span class="line">  orm.RegisterDataBase(<span class="string">"default"</span>,  <span class="string">"mysql"</span>,  <span class="string">"root:root@tcp(127.0.0.1:3306)/go?charset=utf8"</span>)</span><br><span class="line">&#125;</span><br><span class="line">func  main()  &#123;</span><br><span class="line">  beego.Run()</span><br><span class="line">&#125;</span><br><span class="line">定义路由 router.go</span><br><span class="line"></span><br><span class="line">beego.Router(<span class="string">"/type"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:Type"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifyinsert"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"POST:ClassifyInsert"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifyupdate"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:ClassifyUpdate"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifysave"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"POST:ClassifySave"</span>)</span><br><span class="line">beego.Router(<span class="string">"/classifydel"</span>, &amp;controllers.ClassifyController&#123;&#125;, <span class="string">"GET:ClassifyDel"</span>)</span><br><span class="line">model书写</span><br><span class="line"></span><br><span class="line">package  models</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">)</span><br><span class="line">type  Classify  struct  &#123;</span><br><span class="line">  Id  int</span><br><span class="line">  Name  string</span><br><span class="line">  Content  string</span><br><span class="line">&#125;</span><br><span class="line">func  init()  &#123;</span><br><span class="line">  orm.RegisterModel(<span class="keyword">new</span>(Classify))</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//增加操作</span></span><br><span class="line">func  InsertClassify(name  string,  content  string)  (int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  clasinfo  Classify</span><br><span class="line">  clasinfo.Name  =  name</span><br><span class="line">  clasinfo.Content  =  content</span><br><span class="line">  id,  <span class="attr">err</span>  :=  o.Insert(&amp;clasinfo)</span><br><span class="line">  <span class="keyword">return</span>  id,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//删除操作</span></span><br><span class="line">func  ClassifyDel(id  int)  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).Filter(<span class="string">"Id"</span>,  id).Delete()</span><br><span class="line">  o.QueryTable(<span class="string">"inventory"</span>).Filter(<span class="string">"Cid"</span>,  id).Delete()</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//展示</span></span><br><span class="line">func  ClassifyList()  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).All(&amp;lists)</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改</span></span><br><span class="line">func  ClassifyUpdate(id  int,  name  string,  content  string)  ([]Classify,  int64,  error)  &#123;</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  <span class="keyword">var</span>  lists  []Classify</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.QueryTable(<span class="string">"classify"</span>).Filter(<span class="string">"Id"</span>,  id).Update(orm.Params&#123;</span><br><span class="line">  <span class="string">"name"</span>:  name,</span><br><span class="line">  <span class="string">"content"</span>:  content,</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="keyword">return</span>  lists,  num,  err</span><br><span class="line">&#125;</span><br><span class="line">控制器</span><br><span class="line"></span><br><span class="line">package  controllers</span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">  <span class="string">"newgo/models"</span></span><br><span class="line">  <span class="string">"strings"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego/orm"</span></span><br><span class="line">  <span class="string">"github.com/astaxie/beego"</span></span><br><span class="line">)</span><br><span class="line">type  ClassifyController  struct  &#123;</span><br><span class="line">  beego.Controller</span><br><span class="line">&#125;</span><br><span class="line">func  (c  *ClassifyController)  Get()  &#123;</span><br><span class="line">  c.TplName  =  <span class="string">"index.html"</span></span><br><span class="line">&#125;</span><br><span class="line">func  (c  *ClassifyController)  Type()  &#123;</span><br><span class="line">  list,  num,  <span class="attr">err</span>  :=  models.ClassifyList()</span><br><span class="line">  <span class="keyword">if</span>  err  ==  nil  &#123;</span><br><span class="line">  fmt.Println(list)</span><br><span class="line">  fmt.Println(num)</span><br><span class="line">  &#125;</span><br><span class="line">  c.Data[<span class="string">"comment"</span>]  =  list</span><br><span class="line">  fmt.Println(list)</span><br><span class="line">  c.TplName  =  <span class="string">"type.html"</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//添加post</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyInsert()  &#123;</span><br><span class="line">  content  :=  strings.TrimSpace(c.GetString(<span class="string">"content"</span>))</span><br><span class="line">  name  :=  strings.TrimSpace(c.GetString(<span class="string">"name"</span>))</span><br><span class="line">  models.InsertClassify(name,  content)</span><br><span class="line">  c.Ctx.Redirect(<span class="number">302</span>,  <span class="string">"/type"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改get展示</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifySave()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  fmt.Println(id)</span><br><span class="line">  content  :=  strings.TrimSpace(c.GetString(<span class="string">"content"</span>))</span><br><span class="line">  name  :=  strings.TrimSpace(c.GetString(<span class="string">"name"</span>))</span><br><span class="line">  models.ClassifyUpdate(id,  name,  content)</span><br><span class="line">  c.Ctx.Redirect(<span class="number">302</span>,  <span class="string">"/type"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//删除</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyDel()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  models.ClassifyDel(id)</span><br><span class="line">  c.Ctx.WriteString(<span class="string">"200"</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//修改post方法</span></span><br><span class="line">func  (c  *ClassifyController)  ClassifyUpdate()  &#123;</span><br><span class="line">  id,  <span class="attr">_</span>  :=  c.GetInt(<span class="string">"id"</span>)</span><br><span class="line">  o  :=  orm.NewOrm()</span><br><span class="line">  type  Comment  struct  &#123;</span><br><span class="line">  Id  int</span><br><span class="line">  Name  string</span><br><span class="line">  Content  string</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">var</span>  comments  []Comment</span><br><span class="line">  <span class="keyword">var</span>  com  []Comment</span><br><span class="line">  num,  <span class="attr">err</span>  :=  o.Raw(<span class="string">"select  *  from  classify  where  id=?"</span>,  id).QueryRows(&amp;comments)</span><br><span class="line">  o.Raw(<span class="string">"select  *  from  classify"</span>).QueryRows(&amp;com)</span><br><span class="line">  <span class="keyword">if</span>  err  ==  nil  &#123;</span><br><span class="line">  fmt.Println(num)</span><br><span class="line">  fmt.Println(comments)</span><br><span class="line">  fmt.Println(com)</span><br><span class="line">  c.Data[<span class="string">"updates"</span>]  =  comments</span><br><span class="line">  c.Data[<span class="string">"comment"</span>]  =  com</span><br><span class="line">  &#125;  <span class="keyword">else</span>  &#123;</span><br><span class="line">  fmt.Println(<span class="string">"查询报错"</span>)</span><br><span class="line">  &#125;</span><br><span class="line">  c.TplName  =  <span class="string">"type.html"</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="php-go的类"><a href="#php-go的类" class="headerlink" title="php go的类"></a>php go的类</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line">&lt;?php</span><br><span class="line">namespace App;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Animal</span></span></span><br><span class="line"><span class="class"></span>&#123;</span><br><span class="line">    public $name;</span><br><span class="line">    public $height;</span><br><span class="line">    public $weight;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">__construct</span>(<span class="params">$name, $height, $weight</span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;name = $name;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;height = $height;</span><br><span class="line">        $<span class="keyword">this</span>-&gt;weight = $weight;</span><br><span class="line">    &#125;</span><br><span class="line">    public <span class="function"><span class="keyword">function</span> <span class="title">getInfo</span>(<span class="params"></span>)</span></span><br><span class="line"><span class="function">    </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            <span class="string">'name'</span>   =&gt; $<span class="keyword">this</span>-&gt;name,</span><br><span class="line">            <span class="string">'height'</span> =&gt; $<span class="keyword">this</span>-&gt;height,</span><br><span class="line">            <span class="string">'weight'</span> =&gt; $<span class="keyword">this</span>-&gt;weight,</span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26056</span></span><br><span class="line">package main</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Height uint</span><br><span class="line">    Weight uint</span><br><span class="line">&#125;</span><br><span class="line">func NewAnimal(name string, height, weight uint) Animal &#123;</span><br><span class="line">    <span class="keyword">return</span> Animal&#123;</span><br><span class="line">        Name: name,</span><br><span class="line">        Height: height,</span><br><span class="line">        Weight: weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="keyword">return</span> map[string]interface&#123;&#125;&#123;</span><br><span class="line">        <span class="string">"name"</span>: a.Name,</span><br><span class="line">        <span class="string">"height"</span>: a.Height,</span><br><span class="line">        <span class="string">"weight"</span>: a.Weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="Go-for-PHP-Developers-Structs-vs-Classes"><a href="#Go-for-PHP-Developers-Structs-vs-Classes" class="headerlink" title="Go for PHP Developers: Structs vs Classes"></a>Go for PHP Developers: Structs vs Classes</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Height uint</span><br><span class="line">    Weight uint</span><br><span class="line">&#125;</span><br><span class="line">func NewAnimal(name string, height, weight uint) Animal &#123;</span><br><span class="line">    <span class="keyword">return</span> Animal&#123;</span><br><span class="line">        Name: name,</span><br><span class="line">        Height: height,</span><br><span class="line">        Weight: weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/26056#reply85350 定义不同类似的结构体</span></span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="keyword">return</span> map[string]interface&#123;&#125;&#123;</span><br><span class="line">        <span class="string">"name"</span>: a.Name,</span><br><span class="line">        <span class="string">"height"</span>: a.Height,</span><br><span class="line">        <span class="string">"weight"</span>: a.Weight,</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">package main</span><br><span class="line">type AnimalContract interface &#123;</span><br><span class="line">    GetInfo() map[string]interface&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    <span class="comment">// Struct def...</span></span><br><span class="line">&#125;</span><br><span class="line">func (a *Animal) GetInfo() map[string]interface&#123;&#125; &#123;</span><br><span class="line">    <span class="comment">// Method body... 继承AnimalContract 如果我们定义一个定义该接口的结构，它将隐式继承它。</span></span><br><span class="line">&#125;</span><br><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line">func main()&#123;</span><br><span class="line">   <span class="keyword">var</span> array = [<span class="number">9</span>]interface&#123;&#125;&#123;<span class="number">1</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="string">"index"</span>&#125;</span><br><span class="line">   fmt.Print(array)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="append-扩容"><a href="#append-扩容" class="headerlink" title="append 扩容"></a>append 扩容</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">package array</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * arr 底层扩容知识点https://learnku.com/articles/27630#topnav</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line">func ArrayAppend() []int &#123;</span><br><span class="line">    arr := make([]int,<span class="number">5</span>)</span><br><span class="line">    fmt.Printf(<span class="string">"arr.len: %d; arr.cap: %d \n"</span>, len(arr),cap(arr))</span><br><span class="line">    arr = append(arr,<span class="number">10</span>)</span><br><span class="line">    <span class="comment">//问现在 arr 结果是什么</span></span><br><span class="line">    fmt.Printf(<span class="string">"arr.len: %d; arr.cap: %d \n"</span>, len(arr),cap(arr))</span><br><span class="line">    <span class="keyword">return</span> arr</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-build"><a href="#go-build" class="headerlink" title="go build"></a>go build</h3><figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">工作区是 Go 便捷管理项目的方式。一言以蔽之，它就是你系统上的一个目录，Go 可以在此 查找源码 文件，管理依赖包 还有 分发二进制文件。 当 Go 程序命中 <span class="keyword">import</span> 语句，它就会去 Go 标准库 (*$GOROOT/src*) 中寻找响应的包。如果没找到，然后 Go 就会引用环境变量 GOPATH ，它是 Go 工作区目录的路径，然后再去 $GOPATH/src 目录下寻找这个包。</span><br><span class="line"></span><br><span class="line">你可以按需指定多个工作区，只要你保证 GOPATH 环境变量指向你的工作区目录即可。</span><br><span class="line">一个 Go 工作区目录必须有三个子目录也就是: src, pkg and bin</span><br><span class="line">src 目录包含 Package. Package 包括一个包含 Go 源代码 (*.go* files) 。 任何使用 <span class="string">'go get'</span> 命令安装的包也将驻留在这里 (及其依赖包)。</span><br><span class="line"></span><br><span class="line">在 Go 中，每个程序都包含在一个包中。 因此，无论何时您将使用新的 Go 项目，您都需要在 $GOPATH/src 中创建新目录并向上工作。</span><br><span class="line">当执行 go run hello.go 命令时，Go 编译器会首先编译 hello.go 文件，然后执行里面的二进制代码。</span><br><span class="line"></span><br><span class="line">Go 程序支持输出二进制文件，执行 go build &lt;package-name&gt; (main 包) 或 go build program/path.go 命令即可在当前文件夹创建二进制文件。</span><br><span class="line">执行 go install 命令就可以创建这些文件。 go install 命令会触发底层的 go build 命令，然后将这些文件保存到 bin 目录中。通常情况下，这个目录是在系统的可执行路径中。因此，这个目录中的所有文件都是可以通过终端来操作的。</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/26863</span></span><br><span class="line">go run main.go </span><br><span class="line">go build main.go</span><br><span class="line">go install test #main.go在test目录</span><br></pre></td></tr></table></figure>
<h3 id="for-循环迭代变量"><a href="#for-循环迭代变量" class="headerlink" title="for 循环迭代变量"></a>for 循环迭代变量</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">		fmt.Println(&amp;v)</span><br><span class="line">		slice = append(slice, func()&#123;</span><br><span class="line">			fmt.Println(v * v) <span class="comment">// 直接打印结果</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//v 在 for 循环引进的一个块作用域内进行声明。在循环里创建的所有函数变量共享相同的变量，就是一个可访问的存储位置，而不是固定的值。(你会惊奇的发现 &amp;v 的内存地址是一样的)https://www.njphper.com/posts/974e86a6.html</span></span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">		val()</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 25 25 25 25 25</span></span><br><span class="line"><span class="keyword">var</span> slice []func()</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">	sli := []int&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>&#125;</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">v</span> := range sli &#123;</span><br><span class="line">	    temp := v <span class="comment">// 其实很简单 引入一个临时局部变量就可以了，这样就可以将每次的值存储到该变量地址上</span></span><br><span class="line">		fmt.Println(&amp;temp) <span class="comment">// 这里内存地址是不同的</span></span><br><span class="line">		slice = append(slice, func()&#123;</span><br><span class="line">			fmt.Println(temp *  temp) <span class="comment">// 直接打印结果</span></span><br><span class="line">		&#125;)</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">val</span>  := range slice &#123;</span><br><span class="line">		val()</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 输出 1, 4, 9, 16, 25 预期结果</span></span><br></pre></td></tr></table></figure>
<h3 id="实现-PHP-的密码加密解密"><a href="#实现-PHP-的密码加密解密" class="headerlink" title="实现 PHP 的密码加密解密"></a>实现 PHP 的密码加密解密</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line">#https://www.njphper.com/posts/f3f0ab9c.html</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"log"</span></span><br><span class="line">    <span class="string">"golang.org/x/crypto/bcrypt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">for</span> &#123;</span><br><span class="line">       <span class="comment">// 输入密码 获取 hash 值</span></span><br><span class="line">        pwd := getPwd()</span><br><span class="line">        hash := hashAndSalt(pwd)</span><br><span class="line">       <span class="comment">// 再次输入密码验证</span></span><br><span class="line">        pwd2 := getPwd()</span><br><span class="line">        pwdMatch := comparePasswords(hash, pwd2)</span><br><span class="line">        fmt.Println(<span class="string">"Passwords Match?"</span>, pwd)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func getPwd() []byte &#123;</span><br><span class="line">    fmt.Println(<span class="string">"Enter a password"</span>)</span><br><span class="line">    <span class="keyword">var</span> pwd string</span><br><span class="line">    _, <span class="attr">err</span> := fmt.Scan(&amp;pwd)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> []byte(pwd)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">func hashAndSalt(pwd []byte) string &#123;</span><br><span class="line">    hash, <span class="attr">err</span> := bcrypt.GenerateFromPassword(pwd, bcrypt.MinCost)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> string(hash)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func comparePasswords(hashedPwd string, plainPwd []byte) bool &#123;</span><br><span class="line">    byteHash := []byte(hashedPwd)</span><br><span class="line"></span><br><span class="line">    err := bcrypt.CompareHashAndPassword(byteHash, plainPwd)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Println(err)</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line">str := <span class="string">`&#123;"page": 1, "fruits": ["apple", "peach"]&#125;`</span></span><br><span class="line">实例化了一个 json 字符串，fruits 对应的是一个数组。</span><br><span class="line">在 Go 语言中，字符串字面量可以使用双引号 <span class="string">""</span> 或者反引号 <span class="string">' 来创建</span></span><br></pre></td></tr></table></figure>
<h3 id="截取中文"><a href="#截取中文" class="headerlink" title="截取中文"></a>截取中文</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">first := <span class="string">"fisrt"</span></span><br><span class="line">fmt.Println([]rune(first))</span><br><span class="line">fmt.Println([]byte(first))</span><br><span class="line">[]rune(s), 它可以将字符串转化成 unicode 码点</span><br><span class="line">byte 表示一个字节，rune 表示四个字节</span><br><span class="line">first := <span class="string">"社区"</span></span><br><span class="line">fmt.Println([]rune(first))</span><br><span class="line">fmt.Println([]byte(first))</span><br><span class="line">[<span class="number">31038</span> <span class="number">21306</span>] <span class="comment">//输出结果[]rune</span></span><br><span class="line">[<span class="number">231</span> <span class="number">164</span> <span class="number">190</span> <span class="number">229</span> <span class="number">140</span> <span class="number">186</span>]<span class="comment">//输出结果[]byte</span></span><br><span class="line">s := <span class="string">"截取中文"</span></span><br><span class="line"><span class="comment">//试试这样能不能截取?</span></span><br><span class="line">fmt.Println(s[:<span class="number">2</span>])</span><br><span class="line"></span><br><span class="line">s := <span class="string">"截取中文"</span></span><br><span class="line"><span class="comment">//试试这样能不能截取? https://www.njphper.com/posts/c251fcd3.html</span></span><br><span class="line">res := []rune(s)</span><br><span class="line">fmt.Println(string(res[:<span class="number">2</span>]))</span><br></pre></td></tr></table></figure>
<h3 id="动态规划"><a href="#动态规划" class="headerlink" title="动态规划"></a>动态规划</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">硬币问题：如果我们有面值为<span class="number">1</span>元、<span class="number">3</span>元和<span class="number">5</span>元的硬币若干枚，如何用最少的硬币凑够<span class="number">11</span>元？https:<span class="comment">//blog.mutoe.com/2019/dynamic-programming/</span></span><br><span class="line"><span class="comment">// CoinChange: coins 硬币, amount 期望的金额, 返回最少需要的硬币数量，如果不可解返回-1</span></span><br><span class="line">func CoinChange(coins []int, amount int) int &#123;</span><br><span class="line">  dp := make([]int, amount+<span class="number">1</span>)</span><br><span class="line">  dp[<span class="number">0</span>] = <span class="number">0</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> i := <span class="number">1</span>; i &lt;= amount; i++ &#123;</span><br><span class="line">    dp[i] = amount + <span class="number">1</span></span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">coin</span> := range coins &#123;</span><br><span class="line">      <span class="keyword">if</span> coin &lt;= i &amp;&amp; dp[i-coin] != <span class="number">-1</span> &amp;&amp; dp[i-coin]+<span class="number">1</span> &lt; dp[i] &#123;</span><br><span class="line">        dp[i] = dp[i-coin] + <span class="number">1</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> dp[i] &gt; amount &#123;</span><br><span class="line">      dp[i] = <span class="number">-1</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> dp[amount]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-php-对比学习"><a href="#go-php-对比学习" class="headerlink" title="go php 对比学习"></a>go php 对比学习</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//learnku.com/golang/t/23385/from-a-php-engineers-point-of-view-go</span></span><br><span class="line"></span><br><span class="line">first, second = second, first</span><br><span class="line">func getName() (string, string) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">"hello"</span>, <span class="string">"world"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">first, last = getName()</span><br><span class="line"><span class="comment">// foreach ($bookings as $key =&gt; $booking) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> key, <span class="attr">booking</span> := range bookings &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// for ($i = 0; $i &lt; count($bookings); $i++) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> i := <span class="number">0</span>; i &lt; len(bookings); i++ &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// while ($i &lt; count($bookings)) &#123;&#125;</span></span><br><span class="line"><span class="keyword">for</span> i &lt; len(bookings) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// do &#123;&#125; while (true);</span></span><br><span class="line"><span class="keyword">for</span> &#123;&#125;</span><br><span class="line">type rect struct &#123; <span class="comment">// 定义一个结构体</span></span><br><span class="line">    width  int</span><br><span class="line">    height int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (r *rect) area() int &#123; <span class="comment">// 在结构体上添加方法</span></span><br><span class="line">    <span class="keyword">return</span> r.width * r.height</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">r := rect&#123;<span class="attr">width</span>: <span class="number">10</span>, <span class="attr">height</span>: <span class="number">15</span>&#125; <span class="comment">// 初始化</span></span><br><span class="line">fmt.Print(r.area())</span><br><span class="line">type Employee struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">    Job  Job</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Job struct &#123;</span><br><span class="line">    Employer string</span><br><span class="line">    Position string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 并去结构化它</span></span><br><span class="line">e := Employee&#123;</span><br><span class="line">    Name: <span class="string">"Sobit"</span>,</span><br><span class="line">    Job: &#123;</span><br><span class="line">        Employer: <span class="string">"GetYourGuide"</span>,</span><br><span class="line">        Position: <span class="string">"Software Engineer"</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">&#125;func heartbeat() &#123;</span><br><span class="line">     <span class="keyword">for</span> &#123;</span><br><span class="line">         time.Sleep(time.Second)</span><br><span class="line">         fmt.Println(<span class="string">"I'm still running..."</span>)</span><br><span class="line">     &#125;</span><br><span class="line"> &#125;</span><br><span class="line"> 现在， 我们怎样才能让这个函数在后台执行并且允许我们并行的做其他事呢？ 答案比你想象中要简单， 只需要在执行函数前加一个 go :</span><br><span class="line"> </span><br><span class="line"> go heartbeat()</span><br><span class="line"> </span><br><span class="line"> <span class="comment">// 继续做其他事</span></span><br><span class="line"> Go 自带开箱即用关注代码风格的 go fmt 工具。 不再需要分享 IDE 的配置文件， 不用尝试记住大括号应该是在同一行还是再起一行。</span><br><span class="line"> </span><br><span class="line"> 可以使用 go doc 去阅读源码包文档， 而 go vet 将会协助我们查找代码中的问题。 安装第三方包只需要执行 go get [github.com/[vendor]/[package](http:<span class="comment">//github.com/[vendor]/[package)] 指令， 测试只需要执行 go test [package] 指令。</span></span><br></pre></td></tr></table></figure>
<h3 id="用Golang写爬虫"><a href="#用Golang写爬虫" class="headerlink" title="用Golang写爬虫"></a>用Golang写爬虫</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"io/ioutil"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line">func main() &#123;</span><br><span class="line">	fmt.Println(<span class="string">"Hello, world"</span>)</span><br><span class="line">	url := <span class="string">"http://www.baidu.com/"</span></span><br><span class="line">	download(url)</span><br><span class="line">&#125;</span><br><span class="line">func download(url string) &#123;</span><br><span class="line">	client := &amp;http.Client&#123;&#125;</span><br><span class="line">	req, <span class="attr">_</span> := http.NewRequest(<span class="string">"GET"</span>, url, nil)</span><br><span class="line">	<span class="comment">// 自定义Header</span></span><br><span class="line">	req.Header.Set(<span class="string">"User-Agent"</span>, <span class="string">"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"</span>)</span><br><span class="line">	resp, <span class="attr">err</span> := client.Do(req)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"http get error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//函数结束后关闭相关链接https://zhangslob.github.io/2019/01/16/Golang%E5%86%99%E7%88%AC%E8%99%AB/</span></span><br><span class="line">	defer resp.Body.Close()</span><br><span class="line">	body, <span class="attr">err</span> := ioutil.ReadAll(resp.Body)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"read error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	fmt.Println(string(body))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">	<span class="string">"fmt"</span></span><br><span class="line">	<span class="string">"github.com/jackdanger/collectlinks"</span></span><br><span class="line">	<span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line">func main() &#123;</span><br><span class="line">	fmt.Println(<span class="string">"Hello, world"</span>)</span><br><span class="line">	url := <span class="string">"http://www.baidu.com/"</span></span><br><span class="line">	download(url)</span><br><span class="line">&#125;</span><br><span class="line">func download(url string) &#123;</span><br><span class="line">	client := &amp;http.Client&#123;&#125;</span><br><span class="line">	req, <span class="attr">_</span> := http.NewRequest(<span class="string">"GET"</span>, url, nil)</span><br><span class="line">	<span class="comment">// 自定义Header</span></span><br><span class="line">	req.Header.Set(<span class="string">"User-Agent"</span>, <span class="string">"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"</span>)</span><br><span class="line">	resp, <span class="attr">err</span> := client.Do(req)</span><br><span class="line">	<span class="keyword">if</span> err != nil &#123;</span><br><span class="line">		fmt.Println(<span class="string">"http get error"</span>, err)</span><br><span class="line">		<span class="keyword">return</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//函数结束后关闭相关链接</span></span><br><span class="line">	defer resp.Body.Close()</span><br><span class="line">	links := collectlinks.All(resp.Body)</span><br><span class="line">	<span class="keyword">for</span> _, <span class="attr">link</span> := range links &#123;</span><br><span class="line">		fmt.Println(<span class="string">"parse url"</span>, link)</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="go-实现一个简单的-mvc"><a href="#go-实现一个简单的-mvc" class="headerlink" title="go 实现一个简单的 mvc"></a>go 实现一个简单的 mvc</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git clone https:<span class="comment">//github.com/jc91715/go-simple-mvc</span></span><br><span class="line">go get github.com/astaxie/beego</span><br><span class="line">go get github.com/go-sql-driver/mysql</span><br><span class="line"></span><br><span class="line">go run main.go</span><br><span class="line">https:<span class="comment">//learnku.com/articles/29546</span></span><br></pre></td></tr></table></figure>
<h3 id="时间戳操作大全"><a href="#时间戳操作大全" class="headerlink" title="时间戳操作大全"></a>时间戳操作大全</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">dateTime := time.Now()</span><br><span class="line">fmt.Println(dateTime)</span><br><span class="line">year := time.Now().Year() <span class="comment">//年</span></span><br><span class="line"></span><br><span class="line">fmt.Println(year)</span><br><span class="line"></span><br><span class="line">month := time.Now().Month() <span class="comment">//月</span></span><br><span class="line">fmt.Println(month)</span><br><span class="line"></span><br><span class="line">fmt.Println(time.Now().Format(<span class="string">"2006-01-02 15:04:05"</span>))</span><br><span class="line">t := time.Date(<span class="number">2014</span>, <span class="number">1</span>, <span class="number">7</span>, <span class="number">5</span>, <span class="number">50</span>, <span class="number">4</span>, <span class="number">0</span>, time.Local).Unix()</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/30925</span></span><br></pre></td></tr></table></figure>
<h3 id="request"><a href="#request" class="headerlink" title="request"></a>request</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br></pre></td><td class="code"><pre><span class="line">package http</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">    <span class="string">"github.com/imroc/req"</span></span><br><span class="line">    logger <span class="string">"xxx"</span> <span class="comment">//logger日志封装</span></span><br><span class="line">    util <span class="string">"xxxx/sdk-utils"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">type method string</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> (</span><br><span class="line">    GET    method = <span class="string">"GET"</span></span><br><span class="line">    POST   method = <span class="string">"POST"</span></span><br><span class="line">    PUT    method = <span class="string">"PUT"</span></span><br><span class="line">    PATCH  method = <span class="string">"PATCH"</span></span><br><span class="line">    DELETE method = <span class="string">"DELETE"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">type Response struct &#123;</span><br><span class="line">    Data []byte</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//反序列化操作</span></span><br><span class="line">func (r *Response) Deserialize(object interface&#123;&#125;) (err error) &#123;</span><br><span class="line">    err = util.JSONUnmarshal(r.Data, object)</span><br><span class="line">    <span class="keyword">return</span> err</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//支持所有类型请求.</span></span><br><span class="line">func Request(method method, url string, v ...interface&#123;&#125;) (response *Response, err error) &#123;</span><br><span class="line">    <span class="keyword">var</span> r *req.Resp</span><br><span class="line">    response = <span class="keyword">new</span>(Response)</span><br><span class="line">    <span class="keyword">switch</span> method &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"GET"</span>:</span><br><span class="line">        r, err = req.Get(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"POST"</span>:</span><br><span class="line">        r, err = req.Post(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"PUT"</span>:</span><br><span class="line">        r, err = req.Put(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"PATCH"</span>:</span><br><span class="line">        r, err = req.Patch(url, v...)</span><br><span class="line">    <span class="keyword">case</span> <span class="string">"DELETE"</span>:</span><br><span class="line">        r, err = req.Delete(url, v...)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    statusCode := r.Response().StatusCode</span><br><span class="line">    <span class="keyword">if</span> statusCode != <span class="number">200</span> &#123;</span><br><span class="line">        err = fmt.Errorf(<span class="string">"http cilent code err: %d"</span>, statusCode)</span><br><span class="line">        <span class="keyword">return</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    response.Data = r.Bytes()</span><br><span class="line">    <span class="keyword">return</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/30935</span></span><br></pre></td></tr></table></figure>
<h3 id="golang代理"><a href="#golang代理" class="headerlink" title="golang代理"></a>golang代理</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> GO111MODULE=on</span><br><span class="line"><span class="keyword">export</span> GOPROXY=https:<span class="comment">//goproxy.io</span></span><br></pre></td></tr></table></figure>
<h3 id="递归打印杨辉三角"><a href="#递归打印杨辉三角" class="headerlink" title="递归打印杨辉三角"></a>递归打印杨辉三角</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    YangHuiTriangle(<span class="number">10</span>)</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/31290</span></span><br><span class="line">func YangHuiTriangle(n int) []int &#123;</span><br><span class="line">    i := n - <span class="number">1</span></span><br><span class="line">    line := make([]int, n)</span><br><span class="line">    <span class="keyword">if</span> i &lt;= <span class="number">0</span> &#123;</span><br><span class="line">        line = []int&#123;<span class="number">1</span>&#125;</span><br><span class="line">        fmt.Println(line)</span><br><span class="line">        <span class="keyword">return</span> line</span><br><span class="line">    &#125;</span><br><span class="line">    down := YangHuiTriangle(i)</span><br><span class="line">    <span class="keyword">for</span> x := <span class="number">0</span>; x &lt; n; x++ &#123;</span><br><span class="line">        <span class="keyword">if</span> x == <span class="number">0</span> &#123;</span><br><span class="line">            line[<span class="number">0</span>] = <span class="number">1</span></span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> x == n<span class="number">-1</span> &#123;</span><br><span class="line">            line[x] = <span class="number">1</span></span><br><span class="line">            <span class="keyword">continue</span></span><br><span class="line">        &#125;</span><br><span class="line">        line[x] = down[x<span class="number">-1</span>] + down[x]</span><br><span class="line">    &#125;</span><br><span class="line">    fmt.Println(line)</span><br><span class="line">    <span class="keyword">return</span> line</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="JSON-解析"><a href="#JSON-解析" class="headerlink" title="JSON 解析"></a>JSON 解析</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"encoding/json"</span></span><br><span class="line">    <span class="string">"time"</span></span><br><span class="line"></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    type Fruit struct &#123;</span><br><span class="line">        Name string <span class="string">`json:"Name"`</span></span><br><span class="line">        PriceTag string <span class="string">`json:"PriceTag"`</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    type FruitBasket struct &#123;</span><br><span class="line">        Name    string </span><br><span class="line">        Fruit   map[string]Fruit</span><br><span class="line">        Id      int64 <span class="string">`json:"ref"`</span><span class="comment">// 声明对应的json key</span></span><br><span class="line">        Created time.Time</span><br><span class="line"></span><br><span class="line">    &#125;    </span><br><span class="line">    jsonData := []byte(<span class="string">`</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">        "Name": "Standard",</span></span><br><span class="line"><span class="string">        "Fruit" : &#123;</span></span><br><span class="line"><span class="string">        "1": &#123;</span></span><br><span class="line"><span class="string">        "Name": "Apple",</span></span><br><span class="line"><span class="string">        "PriceTag": "$1"</span></span><br><span class="line"><span class="string">        &#125;,</span></span><br><span class="line"><span class="string">        "2": &#123;</span></span><br><span class="line"><span class="string">        "Name": "Pear",</span></span><br><span class="line"><span class="string">        "PriceTag": "$1.5"</span></span><br><span class="line"><span class="string">        &#125;</span></span><br><span class="line"><span class="string">        &#125;,</span></span><br><span class="line"><span class="string">        "ref": 999,</span></span><br><span class="line"><span class="string">        "Created": "2018-04-09T23:00:00Z"</span></span><br><span class="line"><span class="string">    &#125;`</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">var</span> basket FruitBasket</span><br><span class="line">    err := json.Unmarshal(jsonData, &amp;basket)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">         fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">for</span> _, <span class="attr">item</span> := range basket.Fruit &#123;</span><br><span class="line">    fmt.Println(item.Name, item.PriceTag)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//https://learnku.com/articles/31320</span></span><br></pre></td></tr></table></figure>
<h3 id="go-的切片"><a href="#go-的切片" class="headerlink" title="go 的切片"></a>go 的切片</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br></pre></td><td class="code"><pre><span class="line">对于 go 语言的数组，copy 和 view 是同时都存在的。</span><br><span class="line"></span><br><span class="line">copy 就是使用这个数组的时候我将这个数组拷贝一份，这样对于数组的增删改，是不会改变原数组的值的</span><br><span class="line">view 由数组执行切片所返回的对象是一个 view，即视图，若我们在视图上操作数组，会改变原数组，</span><br><span class="line">copy 场景</span><br><span class="line"></span><br><span class="line">package  main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span>  (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func  updateArr(arr [<span class="number">5</span>]int)  &#123;</span><br><span class="line">  arr[<span class="number">0</span>]  =  <span class="number">100</span></span><br><span class="line">  fmt.Println(<span class="string">"修改后的arr："</span>, arr)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func  main()  &#123;</span><br><span class="line">  arr3  :=  [...]int&#123;<span class="number">2</span>,  <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>&#125;</span><br><span class="line">  fmt.Println(<span class="string">"原来的："</span>, arr3)</span><br><span class="line">  updateArr(arr3)</span><br><span class="line">  fmt.Println(<span class="string">"再次查看原始的："</span>, arr3)</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">原来的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">修改后的arr： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">再次查看原始的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">如上代码可以看到，我们在 updateArr 里面修改了下标为 <span class="number">0</span> 的值，但是我们输出原始数组的时候，并没有变。这就是对数组 copy。</span><br><span class="line"></span><br><span class="line">view 场景</span><br><span class="line"></span><br><span class="line">func  updateArr(arr []int)  &#123;</span><br><span class="line">  arr[<span class="number">0</span>]  =  <span class="number">100</span></span><br><span class="line">  fmt.Println(<span class="string">"修改后的arr："</span>, arr)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func  main()  &#123;</span><br><span class="line">  arr3  :=  [...]int&#123;<span class="number">2</span>,  <span class="number">4</span>,  <span class="number">5</span>,  <span class="number">6</span>,  <span class="number">7</span>&#125;</span><br><span class="line">  fmt.Println(<span class="string">"原来的："</span>, arr3)</span><br><span class="line">  <span class="comment">// 使用切片</span></span><br><span class="line">  updateArr(arr3[:])</span><br><span class="line">  fmt.Println(<span class="string">"再次查看原始的："</span>, arr3)</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">原来的： [<span class="number">2</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">修改后的arr： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">再次查看原始的： [<span class="number">100</span> <span class="number">4</span> <span class="number">5</span> <span class="number">6</span> <span class="number">7</span>]</span><br><span class="line">为什么 view 能够改变原数组</span><br><span class="line"></span><br><span class="line">虽然 Slice 本身是值类型，但是它内部使用了对数组的指针引用，所以修改切片数据，会将数组原有数据修改掉。</span><br><span class="line"></span><br><span class="line">当然，在理解上面的同时，一定要知道 go 是如何定义一个切片的</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> b []int</span><br><span class="line">所以，在 updateArr 这个函数传参的时候 arr []int 是传切片进去。不然会报错 https:<span class="comment">//learnku.com/articles/32171</span></span><br></pre></td></tr></table></figure>
<h3 id="结构体的值传递和引用传递"><a href="#结构体的值传递和引用传递" class="headerlink" title="结构体的值传递和引用传递"></a>结构体的值传递和引用传递</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">值传递</span><br><span class="line"></span><br><span class="line">type person struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">    gender string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法定义</span></span><br><span class="line">func (p person) setName(name string) &#123;</span><br><span class="line">    p.name = name</span><br><span class="line">    fmt.Println(<span class="string">"person name is:"</span>,  p.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p *person) describe() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"我叫 %v, 我今年 %v 了"</span>, p.name, p.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    p := person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125;</span><br><span class="line">    <span class="comment">// 修改name的值</span></span><br><span class="line">    p.setName(<span class="string">"jury"</span>)</span><br><span class="line">    p.describe()</span><br><span class="line">&#125;</span><br><span class="line">如上，我们在 setName 这个函数的接受者 是 p person。 此时，就相当于把 person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125; 拷贝一份传递过去。</span><br><span class="line">引用传递</span><br><span class="line"></span><br><span class="line">引用传递就是将该值在内存中的存储地址传递过去。</span><br><span class="line"></span><br><span class="line">type person struct &#123;</span><br><span class="line">    name string</span><br><span class="line">    age int</span><br><span class="line">    gender string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 方法定义</span></span><br><span class="line">func (p *person) setName(name string) &#123;</span><br><span class="line">    p.name = name</span><br><span class="line">    fmt.Println(<span class="string">"person name is:"</span>,  p.name)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p *person) describe() &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"我叫 %v, 我今年 %v 了"</span>, p.name, p.age)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    p := person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125;</span><br><span class="line">    <span class="comment">// 修改name的值</span></span><br><span class="line">    p.setName(<span class="string">"jury"</span>)</span><br><span class="line">    p.describe()</span><br><span class="line">&#125;</span><br><span class="line">我们在 setName 这个函数的接受者 是 p *person。 此时，就相当于把 person&#123;<span class="string">"Tom"</span>, <span class="number">22</span>, <span class="string">"man"</span>&#125; 存储地址传递过去。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32233</span></span><br></pre></td></tr></table></figure>
<h3 id="实现继承"><a href="#实现继承" class="headerlink" title="实现继承"></a>实现继承</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">  <span class="string">"fmt"</span></span><br><span class="line">  <span class="string">"strconv"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动物类</span></span><br><span class="line">type Animal struct &#123;</span><br><span class="line">  name string</span><br><span class="line">  subject string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 动物的公共方法</span></span><br><span class="line">func (a *Animal) eat(food string) &#123;</span><br><span class="line">  fmt.Println(a.name + <span class="string">"喜欢吃："</span> + food +<span class="string">",它属于:"</span> + a.subject)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 猫类，继承动物类https://learnku.com/articles/32295#reply102908</span></span><br><span class="line">type Cat struct &#123;</span><br><span class="line">  <span class="comment">// 继承动物的属性和方法</span></span><br><span class="line">  Animal</span><br><span class="line">  <span class="comment">// 猫自己的属性</span></span><br><span class="line">  age int</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 猫类独有的方法 type name struct&#123;&#125; 结构体 就相当于其他语言中的 class 类的概念。</span></span><br><span class="line">func (c Cat) sleep() &#123;</span><br><span class="line">  fmt.Println(c.name + " 今年" + strconv.Itoa(c.age) + "岁了,特别喜欢睡觉")# go 语言中，string + int，如果想要一个字符串，则需要对 int 类型的值转换为 string 类型，然后才能拼接。</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">  <span class="comment">// 创建一个动物类</span></span><br><span class="line">  animal := Animal&#123;<span class="attr">name</span>:<span class="string">"动物"</span>, <span class="attr">subject</span>:<span class="string">"动物科"</span>&#125;</span><br><span class="line">  animal.eat(<span class="string">"肉"</span>)</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 创建一个猫类</span></span><br><span class="line">  cat := Cat&#123;<span class="attr">Animal</span>: Animal&#123;<span class="attr">name</span>:<span class="string">"咪咪"</span>, <span class="attr">subject</span>:<span class="string">"猫科"</span>&#125;,<span class="attr">age</span>:<span class="number">1</span>&#125;</span><br><span class="line">  cat.eat(<span class="string">"鱼"</span>)</span><br><span class="line">  cat.sleep()</span><br><span class="line">&#125;</span><br><span class="line">输出结果：</span><br><span class="line">    动物喜欢吃：肉,它属于:动物科</span><br><span class="line">    咪咪喜欢吃：鱼,它属于:猫科</span><br><span class="line">    咪咪 今年<span class="number">1</span>岁了,特别喜欢睡觉</span><br></pre></td></tr></table></figure>
<p>###<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line">如果之前学过 PHP 或者其他语言，可以知道实现一个接口，不仅需要实现其接口里的方法，也需要使用 implements 显式说明其实现了该接口。</span><br><span class="line">go 则是 duck type，即像鸭子，那它就可以是个鸭子。也就是说，在 go 中，是要是结构体实现了某个接口指定的方法，那它就是实现了这个接口，不需要使用 implements 显式说明。</span><br><span class="line"></span><br><span class="line">现在说一下接口优点，最大的莫过于控制反转和解耦。举个例子，比如设计一个发送短信的服务，我想接入阿里云与腾讯云的短信业务，此时应该如何设计比较好？是在最底层通过 type 分类来区分，还是在上层通过传入不同的服务，来对应发送不同的运营商短信比较优雅？其实看 laravel 框架上的实现就很明白了。</span><br><span class="line"></span><br><span class="line">同时，go 中的多态的特性通过接口来展现的。</span><br><span class="line"></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> <span class="string">"fmt"</span></span><br><span class="line"></span><br><span class="line">type Usb interface &#123;</span><br><span class="line">    Start()</span><br><span class="line">    Stop()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Phone struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p Phone) Start() &#123;</span><br><span class="line">    p.Name = <span class="string">"张三"</span> <span class="comment">//这里对引用传递还是值传递的作用一样生效，所以这里没有变化</span></span><br><span class="line">    fmt.Println(<span class="string">"手机开始工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (p Phone) Stop() &#123;</span><br><span class="line">    fmt.Println(p.Name) <span class="comment">//李四</span></span><br><span class="line">    fmt.Println(<span class="string">"手机停止工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Carame struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (c *Carame) Start() &#123;</span><br><span class="line">    c.Name = <span class="string">"张三"</span> <span class="comment">//这里对引用传递还是值传递的作用一样生效，所以这里有变化</span></span><br><span class="line">    fmt.Println(<span class="string">"相机开始工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line">func (c *Carame) Stop() &#123;</span><br><span class="line">    fmt.Println(c.Name) <span class="comment">//张三</span></span><br><span class="line">    fmt.Println((*c).Name) <span class="comment">//张三</span></span><br><span class="line">    fmt.Println(<span class="string">"相机停止工作了..."</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Computer struct &#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//Usb是前面定义的接口，这里调用的2个函数，在结构体中一定都要有声明过，否则报错。</span></span><br><span class="line">func (com *Computer) Working(usb Usb) &#123;</span><br><span class="line">    usb.Start()</span><br><span class="line">    usb.Stop()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">var</span> phone = Phone&#123;&#125;</span><br><span class="line">    phone.Name = <span class="string">"李四"</span></span><br><span class="line">    <span class="keyword">var</span> carame = Carame&#123;&#125;</span><br><span class="line">    carame.Name = <span class="string">"李四"</span></span><br><span class="line">    <span class="keyword">var</span> computer = Computer&#123;&#125;</span><br><span class="line">    <span class="comment">//这里传入参数是地址还是对象，需要依靠原结构体里定义的方法来判断(比如Phone结构体)。比如Phone结构体中的start方法是p，此时Working传入对象还是地址都可以。如果是*p，则必须传入地址。与com的类型无关。</span></span><br><span class="line">    computer.Working(&amp;phone)</span><br><span class="line">    computer.Working(&amp;carame)</span><br><span class="line">    <span class="comment">//最后输出</span></span><br><span class="line">    <span class="comment">// 手机开始工作了...</span></span><br><span class="line">    <span class="comment">// 李四</span></span><br><span class="line">    <span class="comment">// 手机停止工作了...</span></span><br><span class="line">    <span class="comment">// 相机开始工作了...</span></span><br><span class="line">    <span class="comment">// 张三</span></span><br><span class="line">    <span class="comment">// 相机停止工作了...</span></span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/32266</span></span><br></pre></td></tr></table></figure></p>
<h3 id="面向对象特性"><a href="#面向对象特性" class="headerlink" title="面向对象特性"></a>面向对象特性</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">接口使用 interface 关键字声明，任何实现接口定义方法的类都可以实例化该接口，接口和实现类之间没有任何依赖</span><br><span class="line">type Sayer interface &#123;</span><br><span class="line">    Say(message string)</span><br><span class="line">    SayHi()</span><br><span class="line">&#125;</span><br><span class="line">继承使用组合的方式实现</span><br><span class="line"></span><br><span class="line">type Animal struct &#123;</span><br><span class="line">    Name string</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func (a *Animal) Say(message string) &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Animal[%v] say: %v\n"</span>, a.Name, message)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">type Dog struct &#123;</span><br><span class="line">    Animal</span><br><span class="line">&#125;</span><br><span class="line">Dog 将继承 Animal 的 Say 方法，以及其成员 Name</span><br><span class="line">子类可以重新实现父类的方法</span><br><span class="line"></span><br><span class="line"><span class="comment">// override Animal.Say</span></span><br><span class="line">func (d *Dog) Say(message string) &#123;</span><br><span class="line">    fmt.Printf(<span class="string">"Dog[%v] say: %v\n"</span>, d.Name, message)</span><br><span class="line">&#125;</span><br><span class="line">Dog.Say 将覆盖 Animal.Say</span><br><span class="line"><span class="keyword">var</span> sayer Sayer</span><br><span class="line"></span><br><span class="line">sayer = &amp;Dog&#123;Animal&#123;<span class="attr">Name</span>: <span class="string">"Yoda"</span>&#125;&#125;</span><br><span class="line">sayer.Say(<span class="string">"hello world"</span>)</span><br></pre></td></tr></table></figure>
<h3 id="使用-Proxy-突破网管的限制"><a href="#使用-Proxy-突破网管的限制" class="headerlink" title="使用 Proxy 突破网管的限制"></a>使用 Proxy 突破网管的限制</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//gitee.com/snail/proxy</span></span><br><span class="line">为了绕开公司网络的监测，需要加密本地发往代理服务器的数据，然后由代理服务器解密数据，再发往真正的服务器，因些我们需要两级代理</span><br><span class="line">./proxy keygen -C proxy</span><br><span class="line">执行命令会在当前目录下生成 proxy.key 和 proxy.crt 两个文件，将这两个文件复制到本机的 <span class="built_in">Proxy</span> 安装目录中</span><br><span class="line">在 VPS 上执行命令：</span><br><span class="line">./proxy socks -t tls -p <span class="string">":3333"</span> -C proxy.crt -K proxy.key --daemon --forever --log proxy.log</span><br><span class="line"></span><br><span class="line">本机打开 <span class="built_in">Proxy</span> 安装目录中的 “bootstrap.bat” 文件，将 “proxy.exe ...” 这一行修改为：</span><br><span class="line"></span><br><span class="line">proxy.exe socks -p <span class="string">":3334"</span> -t tcp -T tls -P <span class="string">"IP:3333"</span> -C proxy.crt -K proxy.key --debug</span><br><span class="line">其中小写 <span class="string">"p"</span> 表示本机监听端口，大写 “P” 表示 VPS 端监听地址，小写 “t” 表示本机使用的传输协议，大写 “T” 表示 VPS 端使用的传输协议。保存文件后，双击 “bootstrap.bat” 即可启动 <span class="built_in">Proxy</span>。</span><br><span class="line">https:<span class="comment">//learnku.com/articles/32369</span></span><br></pre></td></tr></table></figure>
<h3 id="go-执行系统命令时不能直接使用管道符"><a href="#go-执行系统命令时不能直接使用管道符" class="headerlink" title="go 执行系统命令时不能直接使用管道符"></a>go 执行系统命令时不能直接使用管道符</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line">方法一（异步）</span><br><span class="line"></span><br><span class="line">func <span class="keyword">async</span>() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    out, <span class="attr">_</span> := cmd1.StdoutPipe()</span><br><span class="line">    <span class="keyword">in</span>, <span class="attr">_</span> := cmd2.StdinPipe()</span><br><span class="line">    cmd2.Stdout = os.Stdout</span><br><span class="line">    go func() &#123;</span><br><span class="line">        defer func() &#123;</span><br><span class="line">            out.Close()</span><br><span class="line">            <span class="keyword">in</span>.Close()</span><br><span class="line">        &#125;()</span><br><span class="line">        io.Copy(<span class="keyword">in</span>, out)</span><br><span class="line">    &#125;()</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    cmd2.Run()</span><br><span class="line">&#125;</span><br><span class="line">方法二（同步）</span><br><span class="line"></span><br><span class="line">func sync() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    <span class="keyword">in</span>, <span class="attr">_</span> := cmd2.StdinPipe()</span><br><span class="line">    cmd1.Stdout = <span class="keyword">in</span></span><br><span class="line">    cmd2.Stdout = os.Stdout</span><br><span class="line">    cmd2.Start()</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    <span class="keyword">in</span>.Close()</span><br><span class="line">    cmd2.Wait()</span><br><span class="line">&#125;</span><br><span class="line">方法三（多个）</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    cmd1 := exec.Command(<span class="string">"ls"</span>, <span class="string">"."</span>)</span><br><span class="line">    cmd2 := exec.Command(<span class="string">"grep"</span>, <span class="string">"go"</span>)</span><br><span class="line">    cmd3 := exec.Command(<span class="string">"grep"</span>, <span class="string">"main"</span>)</span><br><span class="line">    cmd3.Stdout = os.Stdout</span><br><span class="line">    pipe(cmd1, cmd2, cmd3)</span><br><span class="line">    cmd1.Run()</span><br><span class="line">    cmd2.Run()</span><br><span class="line">    cmd3.Run()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func pipe(cmds ...*exec.Cmd) &#123;</span><br><span class="line">    <span class="keyword">for</span> i, <span class="attr">cmd</span> := range cmds &#123;</span><br><span class="line">        <span class="keyword">if</span> i &gt; <span class="number">0</span> &#123;</span><br><span class="line">            out, <span class="attr">_</span> := cmds[i<span class="number">-1</span>].StdoutPipe()</span><br><span class="line">            <span class="keyword">in</span>, <span class="attr">_</span> := cmd.StdinPipe()</span><br><span class="line">            go func() &#123;</span><br><span class="line">                defer func() &#123;</span><br><span class="line">                    out.Close()</span><br><span class="line">                    <span class="keyword">in</span>.Close()</span><br><span class="line">                &#125;()</span><br><span class="line">                io.Copy(<span class="keyword">in</span>, out)</span><br><span class="line">            &#125;()</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/33592</span></span><br></pre></td></tr></table></figure>
<h3 id="实现一个-web-服务器"><a href="#实现一个-web-服务器" class="headerlink" title="实现一个 web 服务器"></a>实现一个 web 服务器</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"io"</span></span><br><span class="line">    <span class="string">"log"</span></span><br><span class="line">    <span class="string">"net/http"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    http.HandleFunc(<span class="string">"/hello"</span>, helloHandler)</span><br><span class="line">    err := http.ListenAndServe(<span class="string">":8000"</span>, nil)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        log.Fatal(<span class="string">"ListenAndServe:"</span>, err.Error())</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func helloHandler(w http.ResponseWriter, r *http.Request) &#123;</span><br><span class="line">    io.WriteString(w, <span class="string">"hello, world!"</span>)</span><br><span class="line">&#125;</span><br><span class="line">$ go run hello.go</span><br><span class="line">$ curl http:<span class="comment">//localhost:8080/hello</span></span><br><span class="line">hello, world!</span><br><span class="line">https:<span class="comment">//learnku.com/golang/wikis/26652</span></span><br></pre></td></tr></table></figure>
<h3 id="Go-爬虫框架"><a href="#Go-爬虫框架" class="headerlink" title="Go 爬虫框架"></a>Go 爬虫框架</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/zhshch2002/goribot  </span></span><br><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"encoding/json"</span></span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"github.com/zhshch2002/goribot"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    s := goribot.NewSpider()</span><br><span class="line">    _ = s.Get(nil, <span class="string">"https://httpbin.org/get?Goribot%20test=hello%20world"</span>, func(r *goribot.Response) &#123;</span><br><span class="line">        m := make(map[string]interface&#123;&#125;)</span><br><span class="line">        err := json.Unmarshal([]byte(r.Text), &amp;m)</span><br><span class="line">        <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">            fmt.Println(err)</span><br><span class="line">        &#125;</span><br><span class="line">        fmt.Println(m)</span><br><span class="line">    &#125;)</span><br><span class="line">    s.Run()</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/golang/t/33980</span></span><br></pre></td></tr></table></figure>
<p>###go命令行<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">go get -u github.com/cosmos72/gomacro</span><br><span class="line">λ gomacro.exe</span><br><span class="line"><span class="comment">// GOMACRO, an interactive Go interpreter with generics and macros</span></span><br><span class="line"><span class="comment">// Copyright (C) 2018-2019 Massimiliano Ghilardi &lt;https://github.com/cosmos72/gomacro&gt;</span></span><br><span class="line"><span class="comment">// License MPL v2.0+: Mozilla Public License version 2.0 or later &lt;http://mozilla.org/MPL/2.0/&gt;</span></span><br><span class="line"><span class="comment">// This is free software with ABSOLUTELY NO WARRANTY.</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">// Type :help for help</span></span><br><span class="line">gomacro&gt; a:=<span class="number">1</span></span><br><span class="line">gomacro&gt; a</span><br><span class="line"><span class="number">1</span>       <span class="comment">// int</span></span><br><span class="line">go get -u github.com/motemen/gore/cmd/gore</span><br><span class="line">❯ gore</span><br><span class="line">gore version <span class="number">0.4</span><span class="number">.1</span>  :help <span class="keyword">for</span> help</span><br><span class="line">gore&gt; a := <span class="number">1</span></span><br><span class="line"><span class="number">1</span></span><br><span class="line">gore&gt; a + <span class="number">2</span></span><br><span class="line"><span class="number">3</span></span><br><span class="line">gore&gt; func Add(a, b int) int &#123; <span class="keyword">return</span> a + b &#125;</span><br><span class="line">gore&gt; Add(<span class="number">1</span>, <span class="number">2</span>)</span><br><span class="line"><span class="number">3</span></span><br></pre></td></tr></table></figure></p>
<h3 id="go-cron-秒"><a href="#go-cron-秒" class="headerlink" title="go cron 秒"></a>go cron 秒</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">package main</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> (</span><br><span class="line">    <span class="string">"fmt"</span></span><br><span class="line">    <span class="string">"time"</span></span><br><span class="line"></span><br><span class="line">    <span class="string">"github.com/robfig/cron"</span></span><br><span class="line">)</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    c := cron.New()</span><br><span class="line"></span><br><span class="line">    c.AddFunc(<span class="string">"*/1 * * * * *"</span>, func() &#123;</span><br><span class="line">        fmt.Println(<span class="string">"every 1 seconds executing"</span>)</span><br><span class="line">    &#125;)</span><br><span class="line"></span><br><span class="line">    go c.Start()</span><br><span class="line">    defer c.Stop()</span><br><span class="line"></span><br><span class="line">    select &#123;</span><br><span class="line">    <span class="keyword">case</span> &lt;-time.After(time.Second * 10):</span><br><span class="line">        return</span><br><span class="line">    &#125;</span><br><span class="line">&#125;https://learnku.com/articles/34456</span><br><span class="line">Github：github.com/robfig/cron</span><br></pre></td></tr></table></figure>
<h3 id="Go-使用反射导出-Excel"><a href="#Go-使用反射导出-Excel" class="headerlink" title="Go 使用反射导出 Excel"></a>Go 使用反射导出 Excel</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">type Record struct &#123;</span><br><span class="line">    Name string <span class="string">`xlsx:"A-姓名"`</span></span><br><span class="line">    Age  int32  <span class="string">`xlsx:"B-年齡"`</span></span><br><span class="line">&#125;</span><br><span class="line">func RefactorWrite(records []*Record) &#123;</span><br><span class="line">    xlsx := excelize.NewFile()</span><br><span class="line">    index := xlsx.NewSheet(<span class="string">"Sheet1"</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">for</span> i, <span class="attr">t</span> := range records &#123;</span><br><span class="line">        d := reflect.TypeOf(t).Elem()</span><br><span class="line">        <span class="keyword">for</span> j := <span class="number">0</span>; j &lt; d.NumField(); j++ &#123;</span><br><span class="line">            <span class="comment">// 设置表头</span></span><br><span class="line">            <span class="keyword">if</span> i == <span class="number">0</span> &#123;</span><br><span class="line">                column := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">0</span>]</span><br><span class="line">                name := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">1</span>]</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">1</span>), name)</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="comment">// 设置内容</span></span><br><span class="line">            column := strings.Split(d.Field(j).Tag.Get(<span class="string">"xlsx"</span>), <span class="string">"-"</span>)[<span class="number">0</span>]</span><br><span class="line">            <span class="keyword">switch</span> d.Field(j).Type.String() &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"string"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).String())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"int32"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Int())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"int64"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Int())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"bool"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Bool())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"float32"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Float())</span><br><span class="line">            <span class="keyword">case</span> <span class="string">"float64"</span>:</span><br><span class="line">                xlsx.SetCellValue(<span class="string">"Sheet1"</span>, fmt.Sprintf(<span class="string">"%s%d"</span>, column, i+<span class="number">2</span>), reflect.ValueOf(t).Elem().Field(j).Float())</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    xlsx.SetActiveSheet(index)</span><br><span class="line">    <span class="comment">// 保存到xlsx中</span></span><br><span class="line">    err := xlsx.SaveAs(<span class="string">"test_write.xlsx"</span>)</span><br><span class="line">    <span class="keyword">if</span> err != nil &#123;</span><br><span class="line">        fmt.Println(err)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">func main() &#123;</span><br><span class="line">    <span class="keyword">var</span> records []*Record\</span><br><span class="line">    records = append(records, &amp;Record&#123;\</span><br><span class="line">       Name: <span class="string">"小明"</span>,\</span><br><span class="line">      Age:  <span class="number">11</span>,\</span><br><span class="line">    &#125;)\</span><br><span class="line">    records = append(records, &amp;Record&#123;\</span><br><span class="line">       Name: <span class="string">"小华"</span>,\</span><br><span class="line">      Age:  <span class="number">12</span>,\</span><br><span class="line">    &#125;)\</span><br><span class="line">    <span class="comment">// 反射写\</span></span><br><span class="line">    RefactorWrite(records)</span><br><span class="line">&#125;</span><br><span class="line">https:<span class="comment">//learnku.com/articles/34635</span></span><br></pre></td></tr></table></figure>
<h3 id="Redis-解析工具"><a href="#Redis-解析工具" class="headerlink" title="Redis 解析工具"></a>Redis 解析工具</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">https:<span class="comment">//github.com/8090Lambert/go-redis-parser</span></span><br><span class="line">go get github.com/<span class="number">8090</span>Lambert/go-redis-parser</span><br><span class="line">$ go-redis-parser -rdb &lt;dump.rdb&gt; -o &lt;gen-file folder&gt; -type &lt;gen-file type, json or csv, <span class="keyword">default</span> csv&gt;</span><br><span class="line"></span><br><span class="line">BigKeys 的输出示例：</span><br><span class="line"></span><br><span class="line"># Scanning the rdb file to find biggest keys</span><br><span class="line"></span><br><span class="line">-------- summary -------</span><br><span class="line"></span><br><span class="line">Sampled <span class="number">6</span> keys <span class="keyword">in</span> the keyspace!</span><br><span class="line">Total key length <span class="keyword">in</span> bytes is <span class="number">17</span></span><br><span class="line"></span><br><span class="line">Biggest string found <span class="string">'s'</span> has <span class="number">1</span> bytes</span><br><span class="line">Biggest   hash found <span class="string">'h'</span> has <span class="number">1</span> fields</span><br><span class="line">Biggest   list found <span class="string">'li'</span> has <span class="number">2</span> items</span><br><span class="line">Biggest sortedset found <span class="string">'zset'</span> has <span class="number">2</span> members</span><br><span class="line">Biggest    set found <span class="string">'set'</span> has <span class="number">2</span> members</span><br><span class="line">Biggest stream found <span class="string">'stream'</span> has <span class="number">3</span> entries</span><br><span class="line"></span><br><span class="line"><span class="number">1</span> string <span class="keyword">with</span> <span class="number">1</span> bytes</span><br><span class="line"><span class="number">1</span> hash <span class="keyword">with</span> <span class="number">1</span> fields</span><br><span class="line"><span class="number">1</span> list <span class="keyword">with</span> <span class="number">2</span> items</span><br><span class="line">https:<span class="comment">//learnku.com/articles/34655</span></span><br></pre></td></tr></table></figure>
<p><a href="https://learnku.com/articles/34641" target="_blank" rel="noopener">批量导出 CSDN 博客并转为 hexo 博客风格</a></p>
<p><a href="https://learnku.com/golang/t/34334" target="_blank" rel="noopener">警惕 Go 编程陷阱</a></p>
<p><a href="https://learnku.com/articles/34621" target="_blank" rel="noopener">golang IoC</a></p>
<p><a href="https://learnku.com/articles/34190#reply110842" target="_blank" rel="noopener">Golang 操作 Redis 的基本方法</a></p>
<p><a href="https://learnku.com/golang/t/33859" target="_blank" rel="noopener">Go Modules 不完全教程</a></p>
<p><a href="https://learnku.com/articles/34299" target="_blank" rel="noopener">Go 获取已插入 U 盘盘符</a></p>
<p><a href="https://learnku.com/golang/t/34095" target="_blank" rel="noopener">单元测试</a></p>
<p><a href="https://github.com/pibigstar/go-demo" target="_blank" rel="noopener">一些例子和小项目</a></p>
<p><a href="https://strconv.com/posts/web-crawler-exercise-1/" target="_blank" rel="noopener">用Golang写爬虫(一)</a></p>
<p><a href="https://github.com/levigross/grequests" target="_blank" rel="noopener"> go 版本的 requests </a></p>
<p><a href="https://strconv.com/posts/go-repl/?" target="_blank" rel="noopener">go命令行</a></p>
<p><a href="https://book.eddycjy.com/golang/talk/golang-relatively-path.html" target="_blank" rel="noopener">go系列教程</a></p>
<p><a href="https://learnku.com/articles/34112" target="_blank" rel="noopener">beego 代码自动生成器</a></p>
<p><a href="https://learnku.com/articles/34091" target="_blank" rel="noopener">Go Modules 使用</a></p>
<p><a href="https://learnku.com/articles/34013" target="_blank" rel="noopener">文件传输管理系统Bigfile </a></p>
<p><a href="https://github.com/1920853199/go-blog" target="_blank" rel="noopener">基于 beego 的，有情怀的博客</a></p>
<p><a href="https://learnku.com/articles/34051" target="_blank" rel="noopener">Go 基础教程–2-基础知识</a></p>
<p><a href="https://learnku.com/golang/t/33901" target="_blank" rel="noopener">Go 语言 RESTful JSON API 创建</a></p>
<p><a href="https://learnku.com/articles/33918" target="_blank" rel="noopener">Golang slice 从源码来理解</a></p>
<p><a href="https://learnku.com/articles/33876" target="_blank" rel="noopener">golang gin + gorm + seesion 完成 Laravel 的 make:auth</a></p>
<p><a href="https://learnku.com/articles/33873" target="_blank" rel="noopener">Golang 学习宝库</a></p>
<p><a href="https://github.com/pibigstar/go-demo" target="_blank" rel="noopener">学习go语言过程中写的一些例子和小项目</a></p>
<p><a href="https://learnku.com/articles/33708" target="_blank" rel="noopener">Go 实现常用设计模式（十）责任链模式</a></p>
<p><a href="https://juejin.im/post/5ce4dc17518825240245be5b" target="_blank" rel="noopener">Go 语言命令概览</a></p>
<p><a href="https://learnku.com/articles/33601" target="_blank" rel="noopener">密码学之对称加密</a></p>
<p><a href="https://www.guaosi.com/2019/03/14/golang-linkedlist/" target="_blank" rel="noopener">golang算法与数据结构</a></p>
<p><a href="https://github.com/xusenlin/ForestBlog" target="_blank" rel="noopener">用来展示 markdown 文档的博客</a></p>
<p><a href="https://learnku.com/articles/32582" target="_blank" rel="noopener">【GoLang 那点事】Go 指针</a></p>
<p><a href="http://www.golang365.com/#/blog/76" target="_blank" rel="noopener">go语言中的接口</a></p>
<p><a href="https://learnku.com/golang/t/32493" target="_blank" rel="noopener">Go 语言面向对象特性</a></p>
<p><a href="https://learnku.com/golang/t/34202" target="_blank" rel="noopener">如何写出优雅的 Golang 代码</a></p>
<p><a href="https://learnku.com/articles/34243" target="_blank" rel="noopener">内部 API 的安全</a></p>
<p><a href="https://gfw.go101.org/article/channel.html" target="_blank" rel="noopener"> Go 语言通道原理</a></p>
<p><a href="https://learnku.com/articles/32316" target="_blank" rel="noopener">Golang 写爬虫</a></p>
<p><a href="https://www.bilibili.com/video/av57667564/" target="_blank" rel="noopener">Golang 新手教程视频</a></p>
<p><a href="https://learnku.com/golang/t/31515" target="_blank" rel="noopener">golang 对接支付宝支付</a></p>
<p><a href="https://learnku.com/articles/31408" target="_blank" rel="noopener">Go 语言入门</a></p>
<p><a href="https://github.com/tianye/sensitive_words" target="_blank" rel="noopener">敏感词匹配</a></p>
<p><a href="https://learnku.com/golang/t/24598" target="_blank" rel="noopener">使用 go 的 gin 和 gorm 框架来构建 RESTful API 微服务</a></p>
<p><a href="https://learnku.com/articles/27591#faa0c7" target="_blank" rel="noopener">httprouter 源码分析</a></p>
<p><a href="https://www.guaosi.com/2019/03/22/golang-maze-breadth-first-search/" target="_blank" rel="noopener">golang算法与数据结构：走迷宫-广度优先算法</a></p>
<p><a href="https://learnku.com/articles/29791" target="_blank" rel="noopener">Swoole 协程与 Go 协程的区别</a></p>
<p><a href="https://learnku.com/articles/29375" target="_blank" rel="noopener">程序员工作以后该如何提升</a></p>
<p><a href="https://learnku.com/articles/29574" target="_blank" rel="noopener">Go 开发基础入门</a></p>
<p><a href="https://github.com/aceld/zinx" target="_blank" rel="noopener">完整视频代码及学习资料-Zinx 框架-Golang 轻量级 TCP 并发服务</a></p>
<p><a href="https://www.ganymedenil.com/2019/05/27/go-standard-library-by-parse-data-separated-by-commas.html" target="_blank" rel="noopener">go基础库之解析以逗号分隔的数据</a></p>
<p><a href="https://jiajunhuang.com/tutorial/golang/index.md" target="_blank" rel="noopener">Golang(Go语言)简明教程</a></p>
<p><a href="https://github.com/izghua/go-blog" target="_blank" rel="noopener">Golang+gin+vue+MySQL blog https:/www.iphpt.com</a></p>
<p><a href="https://www.njphper.com/posts/8b58ea6d.html" target="_blank" rel="noopener">Go Modules 详解</a></p>
<p><a href="https://github.com/xusenlin/ForestBlog" target="_blank" rel="noopener">markdown博客</a></p>
<p><a href="https://github.com/rylio/ytdl" target="_blank" rel="noopener">YouTube download library and CLI written in Go</a></p>
<p><a href="https://github.com/dchaofei/phone-identifies-gender" target="_blank" rel="noopener">根据手机号识性别</a></p>
<p><a href="https://github.com/developer-learning/learning-golang" target="_blank" rel="noopener">Go 学习之路</a></p>
<p><a href="https://www.njphper.com/posts/b2d15c51.html" target="_blank" rel="noopener">Golang 中的面向对象继承</a></p>
<p><a href="https://github.com/broqiang/mdblog" target="_blank" rel="noopener">Laravel 博客替换成了 Go 写的博客</a></p>
<p><a href="https://github.com/360EntSecGroup-Skylar/excelize" target="_blank" rel="noopener">Go 语言 Excel 类库 Excelize</a></p>
<p><a href="https://learnku.com/articles/26817" target="_blank" rel="noopener">Golang 中的面向对象</a></p>
<p><a href="https://learnku.com/articles/27591" target="_blank" rel="noopener">httprouter 源码分析</a></p>
<p><a href="https://learnku.com/articles/26056" target="_blank" rel="noopener">Go for PHP Developers</a></p>
<p><a href="https://github.com/JeffreyBool/array" target="_blank" rel="noopener"> golang 实现的泛型数组</a></p>
<p><a href="https://58hualong.cn/blog/post/liyong-go-laravel-broadcast-shixian-laravel-de-jishi-tongxun" target="_blank" rel="noopener"> go-Laravel-broadcast 实现 Laravel 的即时通讯</a></p>
<p><a href="https://learnku.com/articles/27808#topnav" target="_blank" rel="noopener">Golang 新手可能会踩的 50 个坑</a></p>
<p><a href="https://learnku.com/golang/wikis/27748" target="_blank" rel="noopener">使用 Go 语言的开源项目和公司</a></p>
<p><a href="https://learnku.com/articles/27561" target="_blank" rel="noopener">Go 如何实现 PHP 的密码加密解密</a></p>
<p><a href="https://github.com/dwg255/landlord" target="_blank" rel="noopener">golang 实现斗地主棋牌游戏</a></p>
<p><a href="https://learnku.com/articles/26715" target="_blank" rel="noopener">Go 编写 Web 应用</a></p>
<p><a href="https://github.com/Away0x/gin_weibo" target="_blank" rel="noopener">模仿 Laravel 的项目结构和风格</a></p>
<p><a href="https://broqiang.com/posts/http-client-base" target="_blank" rel="noopener">HTTP 客户端</a></p>
<p><a href="https://golang.google.cn/" target="_blank" rel="noopener">golang国内镜像</a></p>
<p><a href="https://www.alexedwards.net/blog/an-overview-of-go-tooling" target="_blank" rel="noopener">golang工具</a></p>
<p><a href="https://learnku.com/articles/27691" target="_blank" rel="noopener">Golang 使用 Map</a></p>
<p><a href="https://learnku.com/golang/t/27592" target="_blank" rel="noopener">奔跑的 Go</a></p>
<p><a href="https://learnku.com/articles/27561#topnav" target="_blank" rel="noopener">Go 如何实现 PHP 的密码加密解密</a></p>
<p><a href="https://www.zhanggaoyuan.com/article/8" target="_blank" rel="noopener">GOLANG 超大文件读取的两个方案</a></p>
<p><a href="https://learnku.com/articles/25470#topnav" target="_blank" rel="noopener">《 刻意学习 Golang - 标准库源码分析 》</a></p>
<p><a href="https://github.com/EpochCloud/quick" target="_blank" rel="noopener">专为微服务架构定制的高性能网关</a></p>
<p><a href="https://github.com/Kirk-Wang/Hello-Gopher/" target="_blank" rel="noopener">go扫盲</a></p>
<p><a href="http://www.yinwang.org/blog-cn/2014/04/18/golang" target="_blank" rel="noopener">对 Go 语言的综合评价</a></p>
<p><a href="https://learnku.com/golang/t/26104" target="_blank" rel="noopener">miniblink+golang 开发 Windows gui 应用</a></p>
<p><a href="https://huoding.com/2017/06/09/623" target="_blank" rel="noopener">通过实例入门Golang</a></p>
<p><a href="https://go-zh.org/doc/" target="_blank" rel="noopener">中文:文档首页</a></p>
<p><a href="https://learnku.com/articles/27401" target="_blank" rel="noopener">Go Modules 详解使用</a></p>
<p><a href="https://learnku.com/golang/t/28729#replies" target="_blank" rel="noopener">Golang 的轻量级 TCP 并发服务器框架</a></p>
<p><a href="https://github.com/SuperPony/Golang" target="_blank" rel="noopener">Golang 入门指南</a></p>
<p><a href="https://learnku.com/golang/t/24598" target="_blank" rel="noopener">用 go 的 gin 和 gorm 框架来构建 RESTful API 微服务</a></p>
<p><a href="https://hoxis.github.io/resource-heima-go.html" target="_blank" rel="noopener">go语言视频教程</a></p>
<p><a href="https://learnku.com/golang/t/24715#replies" target="_blank" rel="noopener">Golang 新手教程：入门速成指南</a></p>
<p><a href="https://github.com/SuperPony/Golang_Pkg" target="_blank" rel="noopener"> Go 官方包的学习</a></p>
<p><a href="https://github.com/360EntSecGroup-Skylar/excelize" target="_blank" rel="noopener">Go 语言 Excel 文档excelize</a></p>
<p><a href="https://github.com/youngxhui/GinHello" target="_blank" rel="noopener">Gin 学习示例代码</a></p>
<p><a href="https://goproxy.io/zh/" target="_blank" rel="noopener">golang代理</a></p>
<p><a href="https://github.com/jiujuan/go-collection" target="_blank" rel="noopener">go项目收集</a></p>
<p><a href="https://www.bookstack.cn/read/The-Golang-Standard-Library-by-Example/summary.md" target="_blank" rel="noopener">Go语言标准库</a></p>
<p><a href="https://github.com/dingdayu/phonedata" target="_blank" rel="noopener">手机归属地查询包</a></p>
<p><a href="https://learnku.com/articles/33318#reply106334" target="_blank" rel="noopener">go beego学习</a></p>
<p><a href="http://blog.rickiyang.cn/posts/42334b44.html" target="_blank" rel="noopener">Go基础语法学习(1)</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/golang/" rel="tag"># golang</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/03/07/mysql开发规范/" rel="next" title="mysql开发规范">
                <i class="fa fa-chevron-left"></i> mysql开发规范
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/03/19/leetcode-题解/" rel="prev" title="leetcode 题解">
                leetcode 题解 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#interface-接口"><span class="nav-number">1.</span> <span class="nav-text">interface 接口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Go-陷阱之-for-循环迭代变量"><span class="nav-number">2.</span> <span class="nav-text">Go 陷阱之 for 循环迭代变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Struct-与-面向对象"><span class="nav-number">3.</span> <span class="nav-text">Struct 与 面向对象</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#异常处理"><span class="nav-number">4.</span> <span class="nav-text">异常处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#rand-Intn-生成的是伪随机数"><span class="nav-number">5.</span> <span class="nav-text">rand.Intn () 生成的是伪随机数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#beego-框架-model-curd"><span class="nav-number">6.</span> <span class="nav-text">beego 框架 model curd</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#php-go的类"><span class="nav-number">7.</span> <span class="nav-text">php go的类</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Go-for-PHP-Developers-Structs-vs-Classes"><span class="nav-number">8.</span> <span class="nav-text">Go for PHP Developers: Structs vs Classes</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#append-扩容"><span class="nav-number">9.</span> <span class="nav-text">append 扩容</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-build"><span class="nav-number">10.</span> <span class="nav-text">go build</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#for-循环迭代变量"><span class="nav-number">11.</span> <span class="nav-text">for 循环迭代变量</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现-PHP-的密码加密解密"><span class="nav-number">12.</span> <span class="nav-text">实现 PHP 的密码加密解密</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#截取中文"><span class="nav-number">13.</span> <span class="nav-text">截取中文</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#动态规划"><span class="nav-number">14.</span> <span class="nav-text">动态规划</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-php-对比学习"><span class="nav-number">15.</span> <span class="nav-text">go php 对比学习</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#用Golang写爬虫"><span class="nav-number">16.</span> <span class="nav-text">用Golang写爬虫</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-实现一个简单的-mvc"><span class="nav-number">17.</span> <span class="nav-text">go 实现一个简单的 mvc</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#时间戳操作大全"><span class="nav-number">18.</span> <span class="nav-text">时间戳操作大全</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#request"><span class="nav-number">19.</span> <span class="nav-text">request</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#golang代理"><span class="nav-number">20.</span> <span class="nav-text">golang代理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#递归打印杨辉三角"><span class="nav-number">21.</span> <span class="nav-text">递归打印杨辉三角</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#JSON-解析"><span class="nav-number">22.</span> <span class="nav-text">JSON 解析</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-的切片"><span class="nav-number">23.</span> <span class="nav-text">go 的切片</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#结构体的值传递和引用传递"><span class="nav-number">24.</span> <span class="nav-text">结构体的值传递和引用传递</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现继承"><span class="nav-number">25.</span> <span class="nav-text">实现继承</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#面向对象特性"><span class="nav-number">26.</span> <span class="nav-text">面向对象特性</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用-Proxy-突破网管的限制"><span class="nav-number">27.</span> <span class="nav-text">使用 Proxy 突破网管的限制</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-执行系统命令时不能直接使用管道符"><span class="nav-number">28.</span> <span class="nav-text">go 执行系统命令时不能直接使用管道符</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#实现一个-web-服务器"><span class="nav-number">29.</span> <span class="nav-text">实现一个 web 服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Go-爬虫框架"><span class="nav-number">30.</span> <span class="nav-text">Go 爬虫框架</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#go-cron-秒"><span class="nav-number">31.</span> <span class="nav-text">go cron 秒</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Go-使用反射导出-Excel"><span class="nav-number">32.</span> <span class="nav-text">Go 使用反射导出 Excel</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Redis-解析工具"><span class="nav-number">33.</span> <span class="nav-text">Redis 解析工具</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
